﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;


namespace MaxStudio.Concrete.Web.Pages
{
    public partial class NeedPay : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
                WebPartBind.BindMasterDataDDL(this.ddlStatus, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDNeedPayStatus"].ToString()));
                WebPartBind.BindMasterDataDDL(this.ddlNeedPayType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDPayType"].ToString()));
                WebPartBind.BindTrackingDDL(this.ddlTraker);
                BindVender();
            }
        }

        #region Private Method
        private void BindVender()
        {
            RMVenderService rMVenderServiceObj = ServiceFactory.getRMVenderService();
            OperationResult<List<tb_RMVender>> list = rMVenderServiceObj.GetAllRMVender();

            if (list.operationSuccess)
            {
                ddlVender.DataSource = list.obj;
                ddlVender.DataTextField = "Name";
                ddlVender.DataValueField = "Id";
                ddlVender.DataBind();
            }
        }

        private void BindEditVender()
        {
            RMVenderService rMVenderServiceObj = ServiceFactory.getRMVenderService();
            OperationResult<List<tb_RMVender>> list = rMVenderServiceObj.GetAllRMVender();

            if (list.operationSuccess)
            {
                ddlEditVender.DataSource = list.obj;
                ddlEditVender.DataTextField = "Name";
                ddlEditVender.DataValueField = "Id";
                ddlEditVender.DataBind();
            }
        }

        private void BindGridView()
        {
            NeedPayService needPayServiceObj = ServiceFactory.getNeedPayService();
            OperationResult<List<tb_NeedPay>> result = needPayServiceObj.GetAllNeedPay();

            if (result.operationSuccess)
            {
                gvNeedPayList.DataSource = result.obj;
                gvNeedPayList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            WebPartBind.BindMasterDataDDL(this.ddlEditStatus, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDNeedPayStatus"].ToString()));
            WebPartBind.BindTrackingDDL(this.ddlEditTraker);
            BindEditVender();
            NeedPayService needPayServiceObj = ServiceFactory.getNeedPayService();
            OperationResult<tb_NeedPay> resultEdit = needPayServiceObj.GetNeedPayById(id);
            if (resultEdit.operationSuccess == true)
            {
                ddlEditVender.SelectedValue = resultEdit.obj.VenderId.ToString();
                txtEditAmount.Text = resultEdit.obj.Amount.ToString();
                ddlEditTraker.SelectedValue = resultEdit.obj.Tracking.ToString();
                ddlEditStatus.SelectedValue = resultEdit.obj.Status.ToString();
                txtEditDescription.Text = resultEdit.obj.Description;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }


        public string BindVenderNameByVenderId(string venderId)
        {
            string strReturnValue = "";
            OperationResult<tb_RMVender> result = ServiceFactory.getRMVenderService().GetRMVenderById(Int32.Parse(venderId));
            if (result.operationSuccess)
            {
                strReturnValue = result.obj.Name;
            }
            return strReturnValue;
        }

        public string GetUserNameById(string strUserId)
        { 
            int userId = Int32.Parse(strUserId);
            return MasterDateString.GetUserNameById(userId);
        }

        public string GetMasterDataNameById(string strTypeId)
        {
            int masterDataId = Int32.Parse(strTypeId);
            return MasterDateString.GetMasterDataNameById(masterDataId);
        }


        private void BindDetails(int id)
        {
            lblNeedPayId.Text = id.ToString(); 
        }

        private void BindCheckshouldPayList(int id)
        {
            lblDeleteNeedPayId.Text = id.ToString();
            int intNeedPayId = Int32.Parse(id.ToString());

            NeedPayDetailsService detailsServiceObj = ServiceFactory.getNeedPayDetailsService();
            OperationResult<List<tb_NeedPayDetail>> result = detailsServiceObj.GetNeedPayDetailsByNeedPayId(intNeedPayId);
            if (result.operationSuccess == true)
            {
                gvNeedPayDetailsList.DataSource = result.obj;
                gvNeedPayDetailsList.DataBind();
            }

        }

        private void BindNeedPayDetailsList()
        {
            // TODO  current set the page size is 999.
        }

        protected void DeleteNeedPayDetailsRecords(int id)
        {
            NeedPayDetailsService needPayDetailsServiceObj = ServiceFactory.getNeedPayDetailsService();
            tb_NeedPayDetail deleteEntity = new tb_NeedPayDetail();
            deleteEntity.Id = id;
            needPayDetailsServiceObj.DeleteNeedPayDetails(deleteEntity);
            BindCheckshouldPayList(Int32.Parse(lblDeleteNeedPayId.Text));
        }

        public string CalculatorPay(string strNeedPayId)
        {
            int needPayId = Int32.Parse(strNeedPayId);
            NeedPayService needPayServiceObj = ServiceFactory.getNeedPayService();
            OperationResult<tb_NeedPay> needPayResult = needPayServiceObj.GetNeedPayById(needPayId);
            tb_NeedPay needPayEntity = new tb_NeedPay();
            if (needPayResult.operationSuccess)
            {
                needPayEntity = needPayResult.obj;
            }
            decimal totalAmount = needPayEntity.Amount;

            List<tb_NeedPayDetail> DetailsList = new List<tb_NeedPayDetail>();
            NeedPayDetailsService needPayDetailsServiceObj = ServiceFactory.getNeedPayDetailsService();
            OperationResult<List<tb_NeedPayDetail>> DetailsListResult = needPayDetailsServiceObj.GetNeedPayDetailsByNeedPayId(needPayId);
            if (DetailsListResult.operationSuccess)
            {
                DetailsList = DetailsListResult.obj;
            }
            decimal needPayAmount = 0;

            foreach (tb_NeedPayDetail entity in DetailsList)
            {
                needPayAmount = needPayAmount + entity.Amount;
            }

            return needPayAmount.ToString();
        }

        public string CalculatorNotPay(string strNeedPayId)
        {
            int needPayId = Int32.Parse(strNeedPayId);
            NeedPayService needPayServiceObj = ServiceFactory.getNeedPayService();
            OperationResult<tb_NeedPay> needPayResult = needPayServiceObj.GetNeedPayById(needPayId);
            tb_NeedPay needPayEntity = new tb_NeedPay();
            if (needPayResult.operationSuccess)
            {
                needPayEntity = needPayResult.obj;
            }
            decimal totalAmount = needPayEntity.Amount;

            List<tb_NeedPayDetail> DetailsList = new List<tb_NeedPayDetail>();
            NeedPayDetailsService needPayDetailsServiceObj = ServiceFactory.getNeedPayDetailsService();
            OperationResult<List<tb_NeedPayDetail>> DetailsListResult = needPayDetailsServiceObj.GetNeedPayDetailsByNeedPayId(needPayId);
            if (DetailsListResult.operationSuccess)
            {
                DetailsList = DetailsListResult.obj;
            }
            decimal needPayAmount = 0;

            foreach (tb_NeedPayDetail entity in DetailsList)
            {
                needPayAmount = needPayAmount + entity.Amount;
            }
            decimal notPay = totalAmount - needPayAmount;
            return notPay.ToString();
        }
         #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            NeedPayService needPayServiceObj = ServiceFactory.getNeedPayService();
            tb_NeedPay instance = new tb_NeedPay();

            instance.VenderId = Int32.Parse(ddlVender.SelectedValue);
            instance.Amount = decimal.Parse(txtAmount.Text);
            instance.Tracking = Int32.Parse(ddlTraker.SelectedValue);
            instance.Status = Int32.Parse(ddlStatus.SelectedValue);
            instance.Description = txtDescription.Text;

            OperationResult<tb_NeedPay> result = needPayServiceObj.CreateNeedPay(instance);
            BindGridView();
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            NeedPayService needPayServiceObj = ServiceFactory.getNeedPayService();
            tb_NeedPay entity = new tb_NeedPay();

            entity.Id = editId;

            entity.VenderId = Int32.Parse(ddlEditVender.SelectedValue);
            entity.Amount = decimal.Parse(txtEditAmount.Text);
            entity.Tracking = Int32.Parse(ddlEditTraker.SelectedValue);
            entity.Status = Int32.Parse(ddlEditStatus.SelectedValue);
            entity.Description = txtEditDescription.Text;

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_NeedPay> resultEdit = needPayServiceObj.ModifyNeedPay(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            NeedPayService needPayServiceObj = ServiceFactory.getNeedPayService();
            tb_NeedPay deleteEntity = new tb_NeedPay();
            deleteEntity.Id = deleteId;
            needPayServiceObj.DeleteNeedPay(deleteEntity);
            BindGridView();
        }

        protected void gvNeedPayList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "AddDetails")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindDetails(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "CheckshouldPayList")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindCheckshouldPayList(Int32.Parse(strSelectId));
            }
        }

        protected void gvNeedPayList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNeedPayList.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gvNeedPayDetailsList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteNeedPayDetailsRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvNeedPayDetailsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNeedPayDetailsList.PageIndex = e.NewPageIndex;
            BindNeedPayDetailsList();
        }


        protected void btnSubmitDetails_Click(object sender, EventArgs e)
        {
            int intNeedPayId = Int32.Parse(lblNeedPayId.Text);
            int intNeedPayType = Int32.Parse(ddlNeedPayType.SelectedValue);
            decimal decimalAmount = decimal.Parse(txtNeedPayDetailsAmount.Text);

            tb_NeedPayDetail DetailsEntity = new tb_NeedPayDetail();
            DetailsEntity.NeedPayId = intNeedPayId;
            DetailsEntity.NeedPayType = intNeedPayType;
            DetailsEntity.Amount = decimalAmount;

            NeedPayDetailsService needPayDetailsServiceObj = ServiceFactory.getNeedPayDetailsService();

            OperationResult<tb_NeedPayDetail> result = needPayDetailsServiceObj.CreateNeedPayDetails(DetailsEntity);

        }

        #endregion
    }
}
